﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SGRapidForm.Common;
using System.IO;
using DevExpress.Web.ASPxUploadControl;
using SGRapidForm.Permission.Service.Interface;
using System.Xml;
using SGRapidForm.Permission.Entity;

namespace SGDemo.Permission
{
    public partial class ImportPageData : System.Web.UI.Page
    {
        private IPermissionService _permissionService = ServiceProxyFactory.Create<IPermissionService>("permissionservice");

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        const string UploadDirectory = "~/UploadFile/Permission/";

        /// <summary>
        /// 上传文件完成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void uplImage_FileUploadComplete(object sender, FileUploadCompleteEventArgs e)
        {
            e.CallbackData = SavePostedFile(e.UploadedFile);
        }

        /// <summary>
        /// 保存上传的文件
        /// </summary>
        /// <param name="uploadedFile"></param>
        public string SavePostedFile(UploadedFile uploadedFile)
        {
            if (!uploadedFile.IsValid)
            {
                return "{isSucess:false,msg:'上传文件无效'}";
            }

            string fileName = Path.Combine(MapPath(UploadDirectory), uploadedFile.FileName);
            uploadedFile.SaveAs(fileName, true);
            return ImportData(fileName);
        }

        /// <summary>
        /// 导入一个页面的按钮数据
        /// </summary>
        /// <remarks>
        /// 1，检测页面是否存在，存在则删除此页面下的所有按钮信息
        /// 2，获取此页面的Keyid和所属ModuleKeyid
        /// 3，遍历XML按钮节点，添加到按钮表中
        /// 4，删除上传的XML文档
        /// </remarks>
        protected string ImportData(string absolutePath)
        {
            //1,导入前清空数据
 
            _permissionService.Page_ClearBeforeImporting();
            
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(absolutePath);
                XmlElement rootElem = doc.DocumentElement;
                XmlNodeList moduleNodes = rootElem.GetElementsByTagName("Module");

                //2,判断模块是否为空，空则不添加该模块下的页面
                foreach (XmlNode firstLevelNode in moduleNodes)
                {
                    string moduleKeyid = ((XmlElement)firstLevelNode).GetAttribute("Keyid");
                    string moduleCode = _permissionService.Module_GetByKeyid(moduleKeyid).ModuleCode;
                    if (string.IsNullOrEmpty(moduleCode))
                    {
                        continue;
                    }
                    //3,添加页面
                    foreach (XmlNode secondLevelNode in firstLevelNode.ChildNodes)
                    {
                        PageEntity page = new PageEntity();
                        string keyid = ((XmlElement)secondLevelNode).GetAttribute("Keyid");
                        page.Keyid = string.IsNullOrEmpty(keyid) ? Guid.NewGuid().ToString() : keyid;
                        page.PageName = ((XmlElement)secondLevelNode).GetAttribute("PageName");
                        page.PageCode = ((XmlElement)secondLevelNode).GetAttribute("PageCode");
                        page.PageNameEn = ((XmlElement)secondLevelNode).GetAttribute("PageNameEn");

                        page.NavigateUrl = ((XmlElement)secondLevelNode).GetAttribute("NavigateUrl");
                        page.ImageUrl = ((XmlElement)secondLevelNode).GetAttribute("ImageUrl");
                        page.Order = int.Parse(((XmlElement)secondLevelNode).GetAttribute("Order"));
                        page.PageType = ((XmlElement)secondLevelNode).GetAttribute("PageType");
                        page.Remark = ((XmlElement)secondLevelNode).GetAttribute("Remark");

                        page.ParentKeyid = ((XmlElement)secondLevelNode).GetAttribute("ParentKeyid");
                        page.ModuleKeyid = moduleKeyid;
                        page.HelpKeyid = ((XmlElement)secondLevelNode).GetAttribute("HelpKeyid");
                        page.HelpTitle = ((XmlElement)secondLevelNode).GetAttribute("HelpTitle");

                        _permissionService.Page_Add(page);
                    }
                }
                //4，删除导入的文件
                File.Delete(absolutePath);
            }
            catch (Exception e)
            {
                return "{isSucess:false,msg:'" + e.Message + "'}";
            }

            return "{isSucess:true,msg:'导入成功！'}";
        }
    }
}